<template>
  <view>
    <canvas :canvas-id="column" :id="column" class="charts" @touchend="tap"/>
  </view>
</template>

<script>
import uCharts from '@/common/charts.js';
var uChartsInstance = {};
export default {
	props:{
		chartData: {
			type: Object,
			default:()=>{} 
		},
		column: {
			type: String,
			default:()=>"column" 
		},	
		yAxis: {
			type: Object,
			default:()=>{
				return {
						  gridType: "dash",
						  dashLength: 2
						}
				
			} 
		},
		dat1: {
			type: Number,
			default:()=> -1 
		},	
		min: {
			type: Number,
			default:()=>0 
		},
		max: {
			type: Number,
			default:()=>300
		},	
		color: {
			type: Array,
			default:()=>["#1890FF","#91CB74","#FAC858","#EE6666","#73C0DE","#3CA272","#FC8452","#9A60B4","#ea7ccc"]
		},
	},
  data() {
    return {
      cWidth: 750,
      cHeight: 500
    };
  },
  watch:{
	dat1(newData){
		this.cWidth = uni.upx2px(700);
		//这里的 500 对应 css .charts 的 height
		this.cHeight = uni.upx2px(340);
		
		this.getServerData();
	}  
  },
  created() {
    //这里的 750 对应 css .charts 的 width
  
  },
  methods: {
    getServerData() {
      //模拟从服务器获取数据时的延时
	  this.drawCharts(this.column, this.chartData);
    },
    drawCharts(id,data){
      const ctx = uni.createCanvasContext(id, this);
      uChartsInstance[id] = new uCharts({
        type: "line",
        context: ctx,
        width: this.cWidth,
        height: this.cHeight,
        categories: data.categories,
        series: data.series,
        animation: true,
        padding: [15,15,0,5],
		legend: {show:false},
        xAxis: {
          disableGrid: true
        },
        yAxis: {
          data: [{min: this.yAxis.min,max:this.yAxis.max}]
        },
        extra: {
          line: {
            type: "straight",
            width: 2,
            activeType: "hollow"
          }
        }
      });
    },
    tap(e){
      uChartsInstance[e.target.id].touchLegend(e);
      uChartsInstance[e.target.id].showToolTip(e);
    }
  }
};
</script>

<style scoped>
  .charts{
	  margin-top:20rpx;
	  margin-bottom:30rpx;
    width: 750rpx;
    height: 340rpx;
  }
</style>